﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;
using Microsoft.Kinect;

namespace KinectPowerPointControl_sample1_Fukidashi
{
  
    static class ProcessSkeletonDrawGesture
    {
        static double diffRadious = 0.012;
        static bool isSkeletonDrawActive = false;
        static bool isNearHandKneeActive = false;

        static Joint hand_right;
        static Joint hand_left;
        static Joint knee_right;
        static Joint knee_left;

        static long lastActivated = 0; //最後にこのジェスチャーが発生したタイミング（100ナノ秒単位のtickで保持）
        static long SLEEP_TIME = 5 * 1000 * 1000 * 10; //発動後、発動を抑える時間

        // ポーズをチェックする。開始と終了を決定する
        // 右手を右ひざ、左手を左ひざに近づけると、開始　または終了
        public static bool checkState(){

            if (DateTime.Now.Ticks - lastActivated > SLEEP_TIME) { lastActivated = 0; } //発動後、発動を抑える時間が経過した場合
            if (  lastActivated > 0)   
            {
                return false;
            }
         
            lastActivated = DateTime.Now.Ticks;
            return isSkeletonDrawActive;
        }

        // スケルトン描画の切り替えをポーズで判定する
        public static void changeSkeletonDrawActive(JointCollectionEquivalent joints)
        {
            if (checkPause(joints) == true )
            {
                if (!isNearHandKneeActive)
                {
                    // スケルトン描画が無効の場合は有効にする
                    if (!isSkeletonDrawActive)
                    {
                        isSkeletonDrawActive = true;
                    }
                    else
                    {
                        // スケルトン描画が有効の場合は無効にする
                        isSkeletonDrawActive = false;
                    }

                    // ひざと手が近いことをアクティブにする
                    isNearHandKneeActive = true;
                }
            }
            else
            {
                isNearHandKneeActive = false;
            }
        }

        // 右手右ひざが近い　かつ　左手左ひざが近い　と　真を返す
        public static bool checkPause(JointCollectionEquivalent joints)
        {

            if (getSkeleton(joints))
            {
                return false;
            }

            double right_radious = System.Math.Pow(hand_right.Position.X - knee_right.Position.X, 2.0) +
                System.Math.Pow(hand_right.Position.Y - knee_right.Position.Y, 2.0);
            double left_radious = System.Math.Pow(hand_left.Position.X - knee_left.Position.X, 2.0) +
                System.Math.Pow(hand_left.Position.Y - knee_left.Position.Y, 2.0);
            if ( right_radious < diffRadious && left_radious < diffRadious)
            {
                System.Diagnostics.Debug.WriteLine(" right_radious = " + right_radious + "\n left_radious = " + left_radious);
                return true;
            }
            return false;
        }


        private static bool getSkeleton(JointCollectionEquivalent joints)
        {
            hand_right = joints[JointType.HandRight];
            hand_left = joints[JointType.HandLeft];
            knee_right = joints[JointType.KneeRight];
            knee_left = joints[JointType.KneeLeft];


            if (hand_left.TrackingState != JointTrackingState.Tracked ||
                hand_right.TrackingState != JointTrackingState.Tracked ||
                knee_left.TrackingState != JointTrackingState.Tracked ||
                knee_right.TrackingState != JointTrackingState.Tracked)
            {
                 return true;
            }
            return false;
        }


        public static Hashtable getPositions(KinectSensor sensor, JointCollectionEquivalent joints)
        {
            Hashtable positions = new Hashtable();
            //ColorImagePoint headPosition = sensor.MapSkeletonPointToColor(skeleton.Joints[JointType.Head].Position, sensor.ColorStream.Format);

            positions["Head"] = sensor.MapSkeletonPointToColor(joints[JointType.Head].Position, sensor.ColorStream.Format);
            positions["HandRight"] = sensor.MapSkeletonPointToColor(joints[JointType.HandRight].Position, sensor.ColorStream.Format);
            positions["HandLeft"] = sensor.MapSkeletonPointToColor(joints[JointType.HandLeft].Position, sensor.ColorStream.Format);
            positions["ShoulderRight"] = sensor.MapSkeletonPointToColor(joints[JointType.ShoulderRight].Position, sensor.ColorStream.Format);
            positions["ShoulderLeft"] = sensor.MapSkeletonPointToColor( joints[JointType.ShoulderLeft].Position, sensor.ColorStream.Format);
            positions["ShoulderCenter"] = sensor.MapSkeletonPointToColor(joints[JointType.ShoulderCenter].Position, sensor.ColorStream.Format);

            positions["HipCenter"] = sensor.MapSkeletonPointToColor(joints[JointType.HipCenter].Position, sensor.ColorStream.Format);
            positions["KneeLeft"] = sensor.MapSkeletonPointToColor(joints[JointType.KneeLeft].Position, sensor.ColorStream.Format);
            positions["KneeRight"] = sensor.MapSkeletonPointToColor(joints[JointType.KneeRight].Position, sensor.ColorStream.Format);
 

            return positions;
        }   
    }
}
